package com.ruoyi.service.impl;

import java.util.List;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.CarConsumptionRecordMapper;
import com.ruoyi.domain.CarConsumptionRecord;
import com.ruoyi.service.ICarConsumptionRecordService;
import java.math.BigDecimal;

/**
 * 消费记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-27
 */
@Service
public class CarConsumptionRecordServiceImpl implements ICarConsumptionRecordService 
{
    @Autowired
    private CarConsumptionRecordMapper carConsumptionRecordMapper;

    /**
     * 查询消费记录
     * 
     * @param id 消费记录主键
     * @return 消费记录
     */
    @Override
    public CarConsumptionRecord selectCarConsumptionRecordById(Integer id)
    {
        return carConsumptionRecordMapper.selectCarConsumptionRecordById(id);
    }

    /**
     * 查询消费记录列表
     * 
     * @param carConsumptionRecord 消费记录
     * @return 消费记录集合
     */
    @Override
    public List<CarConsumptionRecord> selectCarConsumptionRecordList(CarConsumptionRecord carConsumptionRecord)
    {
        return carConsumptionRecordMapper.selectCarConsumptionRecordList(carConsumptionRecord);
    }

    /**
     * 根据用户ID查询消费记录列表
     * 
     * @param userId 用户ID
     * @return 消费记录列表
     */
    @Override
    public List<CarConsumptionRecord> selectCarConsumptionRecordByUserId(Integer userId)
    {
        System.out.println("查询用户ID: " + userId + " 的消费记录");
        
        CarConsumptionRecord query = new CarConsumptionRecord();
        query.setUserId(userId);
        
        List<CarConsumptionRecord> records = carConsumptionRecordMapper.selectCarConsumptionRecordList(query);
        
        System.out.println("查询结果数量: " + (records != null ? records.size() : "null"));
        if (records != null) {
            for (CarConsumptionRecord record : records) {
                System.out.println("记录: ID=" + record.getRecordId() + 
                                 ", 用户ID=" + record.getUserId() + 
                                 ", 金额=" + record.getAmount() + 
                                 ", 备注=" + record.getRemark() + 
                                 ", 时间=" + record.getRecordTime());
            }
        }
        
        return records;
    }

    /**
     * 根据消费类型查询消费记录
     * 
     * @param userId 用户ID
     * @param recordType 记录类型
     * @return 消费记录列表
     */
    @Override
    public List<CarConsumptionRecord> selectCarConsumptionRecordByType(Integer userId, Integer recordType)
    {
        CarConsumptionRecord query = new CarConsumptionRecord();
        query.setUserId(userId);
        query.setRecordType(recordType);
        return carConsumptionRecordMapper.selectCarConsumptionRecordList(query);
    }

    /**
     * 新增消费记录
     * 
     * @param carConsumptionRecord 消费记录
     * @return 结果
     */
    @Override
    public int insertCarConsumptionRecord(CarConsumptionRecord carConsumptionRecord)
    {
        if (carConsumptionRecord.getRecordTime() == null) {
            carConsumptionRecord.setRecordTime(new Date());
        }
        // 移除deleted字段设置，因为数据库表中没有这个字段
        // carConsumptionRecord.setDeleted(0);
        return carConsumptionRecordMapper.insertCarConsumptionRecord(carConsumptionRecord);
    }

    /**
     * 修改消费记录
     * 
     * @param carConsumptionRecord 消费记录
     * @return 结果
     */
    @Override
    public int updateCarConsumptionRecord(CarConsumptionRecord carConsumptionRecord)
    {
        return carConsumptionRecordMapper.updateCarConsumptionRecord(carConsumptionRecord);
    }

    /**
     * 批量删除消费记录
     * 
     * @param ids 需要删除的消费记录主键
     * @return 结果
     */
    @Override
    public int deleteCarConsumptionRecordByIds(Integer[] ids)
    {
        return carConsumptionRecordMapper.deleteCarConsumptionRecordByIds(ids);
    }

    /**
     * 删除消费记录信息
     * 
     * @param id 消费记录主键
     * @return 结果
     */
    @Override
    public int deleteCarConsumptionRecordById(Integer id)
    {
        return carConsumptionRecordMapper.deleteCarConsumptionRecordById(id);
    }

    /**
     * 获取用户消费统计
     * 
     * @param userId 用户ID
     * @return 消费统计信息
     */
    @Override
    public Object getUserConsumptionStatistics(Integer userId)
    {
        try {
            // 获取用户所有消费记录
            List<CarConsumptionRecord> records = selectCarConsumptionRecordByUserId(userId);
            
            Map<String, Object> statistics = new HashMap<>();
            BigDecimal totalAmount = BigDecimal.ZERO;
            BigDecimal monthlyTotal = BigDecimal.ZERO;
            int totalCount = 0;
            int recordCount = 0;
            
            // 获取当前月份（暂时使用所有数据，不考虑月份限制）
            int currentMonth = new Date().getMonth() + 1;
            int currentYear = new Date().getYear() + 1900;
            
            System.out.println("当前时间: " + currentYear + "年" + currentMonth + "月");
            System.out.println("查询用户ID: " + userId + " 的记录数量: " + (records != null ? records.size() : "null"));
            
            for (CarConsumptionRecord record : records) {
                System.out.println("处理记录: " + record.getRecordId() + ", 金额: " + record.getAmount() + ", 时间: " + record.getRecordTime());
                
                if (record.getAmount() != null) {
                    BigDecimal amount = record.getAmount();
                    totalAmount = totalAmount.add(amount);
                    totalCount++;
                    
                    // 检查是否为当前月份
                    if (record.getRecordTime() != null) {
                        Date recordDate = record.getRecordTime();
                        if (isCurrentMonth(recordDate)) {
                            monthlyTotal = monthlyTotal.add(amount);
                            recordCount++;
                        }
                    }
                }
            }
            
            System.out.println("统计结果 - 总金额: " + totalAmount + ", 月度金额: " + monthlyTotal + ", 记录数: " + totalCount);
            
            statistics.put("totalAmount", totalAmount);
            statistics.put("totalCount", totalCount);
            statistics.put("monthlyTotal", monthlyTotal);
            statistics.put("recordCount", recordCount);
            
            return statistics;
        } catch (Exception e) {
            System.err.println("统计过程中出错: " + e.getMessage());
            e.printStackTrace();
            // 如果出错，返回默认值
            Map<String, Object> statistics = new HashMap<>();
            statistics.put("totalAmount", BigDecimal.ZERO);
            statistics.put("totalCount", 0);
            statistics.put("monthlyTotal", BigDecimal.ZERO);
            statistics.put("recordCount", 0);
            return statistics;
        }
    }

    private boolean isCurrentMonth(Date date) {
        Date currentDate = new Date();
        int currentMonth = currentDate.getMonth() + 1;
        int currentYear = currentDate.getYear() + 1900;

        int recordMonth = date.getMonth() + 1;
        int recordYear = date.getYear() + 1900;

        return recordMonth == currentMonth && recordYear == currentYear;
    }
}
